<?php
namespace app\api\controller;
use app\common\controller\Base;

/**
 * 评论、点赞相关
 * 尽量少查询
 */
class CommentPub extends Base{
    protected $tn = 'common/comment';


    

    /**
     * [commentList 评论列表] 
     * @return [type] [description]
     */
    public function commentList(){
        $validate = new \think\validate([
            'type' => 'require|in:1,2,3',
            'foreign_key' => 'require'
        ],[
            'type.require' => '参数type丢失',
            'type.in' => '参数type有误',
            'foreign_key' => '参数foreign_key丢失'
        ]);
        
        if($validate->check($this->post) === false){
            $this->apiReturn(0,$validate->getError());
        }
        //判断是否登录
        if(isset($this->post['token']) && trim($this->post['token']) !== ''){
            $this->user_check(trim($this->post['token']));
            $user_id = UID;
        }else{
            $user_id = 0;
        }
        $where['foreign_key'] = intval($this->post['foreign_key']);
        if(isset($this->post['comment_id']) && intval($this->post['comment_id']) > 0){
            $where['comment_id'] = intval($this->post['comment_id']);
        }else{
            $where['comment_id'] = 0;
        }
        $list = db('comment')->alias('c')->join(['bz_user' => 'u'],'u.id = c.user_id')
            ->field('c.*,nick_name,photo_path')
            ->where($where)
            ->order('c.add_time desc')
            ->limit(($this->p-1)*config('page_size'),config('page_size'))
            ->select();
        $zanModel = db('comment_zan');
        foreach($list as $key => $val){
            $list[$key]['is_zan'] = 0;
            if(isset($user_id) && $user_id > 0){
                if($zanModel->where('user_id',$user_id)->where('comment_id',$val['id'])->find()){
                    $list[$key]['is_zan'] = 1;
                }
            }
            $list[$key]['format_time'] = timeFormat($val['add_time']);

            $list[$key]['image'] = empty($val['image']) ?  [] : explode(',',$val['image']) ;
            $list[$key]['tag'] = $val['tag'] == '' ? [] : explode(',',$val['tag']) ;
        }
        $this->apiReturn(1,'评论列表',$list);
    }
}


    